/*
 * @Author: Otway
 * @Date: 2019-01-09 11:16:08
 * @LastEditors: Otway
 * @LastEditTime: 2019-08-30 18:50:09
 * @copyright: h-visions
 */
import React, { PureComponent } from 'react';
import {
    Row,
    Col,
    Form,
    Input,
    Select,
} from 'antd';
const FormItem = Form.Item;
const { Option } = Select
const formItemLayout = {
    // 抽屉里面Form。二列的布局比例
    labelCol: {
        span: 6
    },
    wrapperCol: {
        span: 18
    }
};
class AddFrom extends PureComponent {
    state = {
        types: null,
        data: [],
        empty: false
    }
    // 挂载后
    componentDidMount() {
        console.log(this.props);
        const { one, formData } = this.props
        const { list } = this.props.CalssData[0]
        if (one) {
            let data = []
            list.forEach((value, index) => {
                if (value.factory === formData.factory) {
                    value.list.forEach((value_1, index_1) => {
                        data.push(value_1.workShop)
                    })
                }
            })
            this.setState({
                data,
            })
        }
    }
    // 判断选中显示隐藏
    selects = (value) => {
        console.log('当前类型选中', value);
        this.setState({
            types: this.state.types = value,
        })
    }
    onSelect = (LabeledValue) => {
        const { list } = this.props.CalssData[0]
        let data = []
        list.forEach((value, index) => {
            if (value.factory === LabeledValue) {
                console.log(value.list);
                value.list.forEach((value_1, index_1) => {
                    data.push(value_1.workShop)
                })
            }
        })
        this.setState({
            data,
            empty: true
        })
    }
    onChange = (a) => {
        const { setFieldsValue } = this.props.form
        const { list } = this.props.CalssData[0]
        let data = []
        list.forEach((value, index) => {
            if (value.factory === a) {
                console.log(value.list);
                value.list.forEach((value_1, index_1) => {
                    data.push(value_1.workShop)
                })
            }
        })
        this.setState({
            data,
            empty: true
        })
        setFieldsValue({ attributeFirst: undefined })
    }
    render() {
        const {
            formData,
            one,
            CalssData,
            form: { getFieldDecorator },
        } = this.props;
        const { data, empty } = this.state
        return (
            <Form >
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'工厂:'}>
                            {getFieldDecorator('factory', {
                                initialValue: one ? formData.factory : '',
                                rules: [
                                    {
                                        required: true,
                                        message: '工厂不能为空'
                                    }
                                ]
                            })(<Select allowClear={true} onChange={this.onChange} onSelect={this.onSelect}>
                                {
                                    CalssData[0].list.map((value, index) => {
                                        return <Option value={value.factory} key={`factory_${index}`}>{value.factory}</Option>
                                    })
                                }
                            </Select>)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'一类车间:'}>
                            {getFieldDecorator('attributeFirst', {
                                initialValue: one ? (!empty ? formData.attributeFirst : null) : '',
                                rules: [
                                    {
                                        required: true,
                                        message: '一类车间不能为空'
                                    }
                                ]
                            })(<Select allowClear={true}>
                                {
                                    data !== undefined && data !== null ? data.map((value, index) => {
                                        return <Option value={value} key={`workShop_${index}`}>{value}</Option>
                                    }) : null
                                }
                            </Select>)}
                        </FormItem>
                    </Col>

                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'二类分类:'}>
                            {getFieldDecorator('attributeSecond', {
                                initialValue: one ? formData.attributeSecond : null,
                                // rules: [
                                //     {
                                //         required: true,
                                //         message: '二类分类为空'
                                //     }
                                // ],
                                getValueFromEvent: (event) => {
                                    return event.target.value.replace(/\s+/g, "")
                                },
                            })(<Input placeholder='输入二类分类' allowClear maxLength={20} />)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'机型:'}>
                            {getFieldDecorator('code', {
                                initialValue: one ? formData.code : null,
                                rules: [
                                    {
                                        required: true,
                                        message: '机型为空'
                                    }
                                ],
                                getValueFromEvent: (event) => {
                                    return event.target.value.replace(/\s+/g, "")
                                },
                            })(<Input placeholder='输入机型' allowClear maxLength={50} />)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'加工部门:'}>
                            {getFieldDecorator('assemblyWorker', {
                                initialValue: one ? formData.assemblyWorker : null,
                                rules: [
                                    {
                                        required: false,
                                        message: ''
                                    }
                                ],
                                getValueFromEvent: (event) => {
                                    return event.target.value.replace(/\s+/g, "")
                                },
                            })(<Input placeholder='输入加工部门' allowClear maxLength={20} />)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'附加信息1:'}>
                            {getFieldDecorator('otherMsg1', {
                                initialValue: one ? formData.otherMsg1 : null,
                                rules: [
                                    {
                                        required: false,
                                        message: '附加信息1为空'
                                    }
                                ],
                                getValueFromEvent: (event) => {
                                    return event.target.value.replace(/\s+/g, "")
                                },
                            })(<Input placeholder='输入附加信息1' allowClear maxLength={20} />)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'附加信息2:'}>
                            {getFieldDecorator('otherMsg2', {
                                initialValue: one ? formData.otherMsg2 : null,
                                rules: [
                                    {
                                        required: false,
                                        message: '附加信息2为空'
                                    }
                                ],
                                getValueFromEvent: (event) => {
                                    return event.target.value.replace(/\s+/g, "")
                                },
                            })(<Input placeholder='输入附加信息2' allowClear maxLength={20} />)}
                        </FormItem>
                    </Col>
                </Row>
            </Form>
        );
    }
}

export default Form.create()(AddFrom);
